function setCookie(key, value) {
    document.cookie = `${key}=${value};`;
}

function deleteCookie(key) {
    document.cookie = `${key}=令妹;expires=${new Date(
        Date.now() - 8 * 3600 * 1000 - 1
    )};`;
}

function getCookie(key) {
    const cookies = document.cookie.split("; ");
    // for (let i = 0; i < cookies.length; i++) {
    //     const [name, value] = cookies[i].split("=");
    //     if (name === key) {
    //         return value;
    //     }
    // }

    // const value = cookies.find((value, index) => {
    //     console.log(value, index);
    //     return value.split("=")[0] === key;
    // });

    const value = cookies.find((value) => value.split("=")[0] === key);
    return value ? value.split("=")[1] : value;
}

function getAllCookies() {
    const arr = document.cookie.split("; "); // [a=123,b=456]

    const mappedArr = arr.map((item) => {
        const [key, value] = item.split("="); //[a,123]
        return { key, value }; //{key:"a",value:"123"}
    }); //[{key:"a",value:"123"},{key:"b",value:"456"}]

    return mappedArr;
}
